home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 June / ccd0605.iso / Software / Freeware / Programare / highlight / highlight-W32GUI-2.2-10b-Setup.exe / {app} / src / preformatter.cpp < prev    next >
C/C++ Source or Header  |  2005-01-05  |  5KB  |  172 lines

  1. /***************************************************************************
  2.                         PreFormatter.cpp  -  description
  3.                              -------------------
  4.     begin                : Mo Jan 03 2005
  5.     copyright            : (C) 2005 by AndrΘ Simon
  6.     email                : andre.simon1@gmx.de
  7.  ***************************************************************************/
  8.  
  9. /***************************************************************************
  10.  *                                                                         *
  11.  *   This program is free software; you can redistribute it and/or modify  *
  12.  *   it under the terms of the GNU General Public License as published by  *
  13.  *   the Free Software Foundation; either version 2 of the License, or     *
  14.  *   (at your option) any later version.                                   *
  15.  *                                                                         *
  16.  ***************************************************************************/
  17.  
  18. #include "preformatter.h"
  19.  
  20. namespace highlight {
  21.  
  22. PreFormatter::PreFormatter(bool wrap, bool replTabs):
  23.  maxLineLength(80),
  24.  index(0),
  25.  numberSpaces(0),
  26.  wsPrefixLength(string::npos),
  27.  hasMore(false),
  28.  indentAfterOpenBraces(true),
  29.  redefineWsPrefix(false),
  30.  wrapLines(wrap),
  31.  replaceTabs(replTabs)
  32. {
  33. }
  34.  
  35. PreFormatter::PreFormatter():
  36.  maxLineLength(80),
  37.  index(0),
  38.  numberSpaces(0),
  39.  wsPrefixLength(string::npos),
  40.  hasMore(false),
  41.  indentAfterOpenBraces(true),
  42.  redefineWsPrefix(false),
  43.  wrapLines(false),
  44.  replaceTabs(false)
  45. {
  46. }
  47.  
  48. PreFormatter::~PreFormatter()
  49. {
  50. }
  51.  
  52. bool PreFormatter::hasMoreLines(){
  53.   return hasMore;
  54. }
  55.  
  56. bool PreFormatter::indentCode(){
  57.   return indentAfterOpenBraces;
  58. }
  59.  
  60. void PreFormatter::setLine(const std::string newLine){
  61.  
  62.   line=newLine;
  63.  
  64.   if (replaceTabs && numberSpaces) {
  65.     size_t tabPos=line.find('\t');
  66.     while (tabPos!=string::npos){
  67.        line.replace(tabPos , 1, numberSpaces - (tabPos % numberSpaces) , ' ');
  68.        tabPos = line.find('\t', tabPos+1);
  69.     }
  70.   }
  71.  
  72.   if (wrapLines){
  73.     wsPrefix.clear();
  74.     index=0;
  75.     wsPrefixLength=string::npos;
  76.     hasMore=true;
  77.     redefineWsPrefix=false;
  78.   }
  79. }
  80.  
  81. std::string  PreFormatter::getNextLine(){
  82.  
  83.    if (!wrapLines){
  84.      hasMore = false;
  85.      return line;
  86.    }
  87.  
  88.    if (!index && line.length() > maxLineLength){ // erster Durchlauf...
  89.       // wenn m÷glich an ÷ffnender Klammer oder Geichheitszeichen ausrichten
  90.       if (indentAfterOpenBraces){
  91.           wsPrefixLength=line.find_first_of(INDENT_MARKERS);
  92.       }
  93.       // sonst die Einrⁿckung der Originalzeile beibehalten
  94.       if (wsPrefixLength==string::npos || wsPrefixLength-index>maxLineLength){
  95.           wsPrefixLength=line.find_first_not_of(WS_CHARS);
  96.       }
  97.       else {
  98.           // wsPrefix in allen neu umgebrochenen Zeilen durch Spaces ersetzen
  99.           redefineWsPrefix=true;
  100.           //  Position hinter ÷ffnende Klammer springen
  101.           wsPrefixLength=line.find_first_not_of(WS_CHARS,wsPrefixLength+1);
  102.       }
  103.  
  104.       if (wsPrefixLength!=string::npos){
  105.         index = wsPrefixLength;
  106.         // Falls Anzahl der Whitespaces am beginn der ersten zeile gr÷▀er
  107.         // als Max. ZeilenlΣnge, Whitespaces verwerfen
  108.         if (wsPrefixLength>maxLineLength){
  109.           wsPrefixLength=0;
  110.           return string();
  111.         }
  112.         else{
  113.            wsPrefix=line.substr(0, wsPrefixLength);
  114.         }
  115.       }
  116.       // Zeile enthaelt nur Whitespace; verwerfen
  117.       else {
  118.        hasMore= false;
  119.        return string();
  120.       }
  121.    } else {
  122.      if (redefineWsPrefix){
  123.        wsPrefix.clear();
  124.        wsPrefix.append(wsPrefixLength, ' ');
  125.      }
  126.      redefineWsPrefix=false;
  127.    }
  128.  
  129.    string resultString;
  130.  
  131.    // Position, ab der rckwaerts nach Umbruchmglichkeit gesucht wird
  132.    unsigned int searchEndPos = maxLineLength - wsPrefixLength;
  133.  
  134.    // letztes Teilstueck der Zeile ausgeben; Parsen beenden
  135.    if (line.length()-index < searchEndPos) {
  136.      hasMore=false;
  137.      resultString=(index>0) ? wsPrefix + line.substr(index) : line.substr(index);
  138.      return resultString;
  139.    }
  140.  
  141.    // Umbrechposition suchen
  142.    size_t lbPos = line.find_last_of(LB_CHARS, index+searchEndPos);
  143.    if (lbPos <= index || lbPos == string::npos) {
  144.      // nichts gefunden, hart umbrechen
  145.      lbPos = index + searchEndPos;
  146.    }
  147.    // Einrⁿckung der Originalzeile erhalten
  148.    resultString+=wsPrefix;
  149.    // Neue Zeile erzeugen
  150.    resultString += line.substr(index, lbPos-index+1);
  151.  
  152.    // Whitespace am neuen Zeilenbeginn ignorieren, ausser beim ersten Durchlauf
  153.    //unsigned int newIndex=StringTools::getNextNonWsPos(line,lbPos+1);
  154.    size_t newIndex=line.find_first_not_of(WS_CHARS, lbPos+1);
  155.    index=(newIndex!=string::npos)?newIndex:line.length();
  156.  
  157.    hasMore=index!=line.length(); // unnoetigen Leerstring vermeiden
  158.  
  159.    return resultString;
  160. }
  161.  
  162. void PreFormatter::setWrappingProperties(unsigned int maxLineLength, bool indentAfterOpenBraces){
  163.     this->maxLineLength = maxLineLength;
  164.     this->indentAfterOpenBraces = indentAfterOpenBraces;
  165. }
  166.  
  167. void PreFormatter::setNumberSpaces(unsigned int num){
  168.     numberSpaces = num;
  169. }
  170.  
  171. }
  172.